//
//  ALUtils.m
//  ios-loan
//
//  Created by mac on 2018/3/28.
//  Copyright © 2018年 hxhg. All rights reserved.
//

#import "ALUtils.h"
#import "ALMacro.h"
#import "AFNetworking.h"

@implementation ALUtils
AL_DEF_SINGLETON(ALUtils)

+(NSString *)filterStr:(NSString *)str{
    //TODO: 这里需要做些字符串过滤的处理
    if ([str isKindOfClass:[NSNumber class]]) {
        str=[NSString stringWithFormat:@"%@",str];
    }
    if ((NSNull *)str==[NSNull null]) {
        return @"";
    }
    if (str==nil) {
        return @"";
    }
    NSLog(@"%@",str);
    //去掉空格
    return [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
+(NSString *)filterAllSpeStr:(NSString *)str{
    //TODO: 这里需要做些字符串过滤的处理
    if ([str isKindOfClass:[NSNumber class]]) {
        str=[NSString stringWithFormat:@"%@",str];
    }
    if ((NSNull *)str==[NSNull null]) {
        return @"";
    }
    if (str==nil) {
        return @"";
    }
    //去掉两边空格
    str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    //去掉中间空格
    str=[str stringByReplacingOccurrencesOfString:@" " withString:@""];
    return str;
}
//通过字体 字符串 获得CGSize
+(CGSize)getSizeByStr:(NSString *)str
              andFont:(UIFont *)theFont{
    CGSize theSize;
    if (isAfterIOS7) {
        NSDictionary *attribute = @{NSFontAttributeName: theFont};
        theSize=[str
                 boundingRectWithSize:CGSizeMake(300, 0)
                 options:NSStringDrawingTruncatesLastVisibleLine |
                 NSStringDrawingUsesLineFragmentOrigin |
                 NSStringDrawingUsesFontLeading
                 attributes:attribute
                 context:nil].size;
    }
    else{
        theSize=[str
                 sizeWithFont:theFont
                 constrainedToSize:CGSizeMake(300, 100)
                 lineBreakMode:NSLineBreakByCharWrapping];
    }
    return theSize;
}
+(CGSize)getSizeByStr:(NSString *)str
              andFont:(UIFont *)theFont
              andRect:(CGSize)rect{
    CGSize theSize;
    if (isAfterIOS7) {
        NSDictionary *attribute = @{NSFontAttributeName: theFont};
        theSize=[str
                 boundingRectWithSize:rect
                 options:NSStringDrawingTruncatesLastVisibleLine |
                 NSStringDrawingUsesLineFragmentOrigin |
                 NSStringDrawingUsesFontLeading
                 attributes:attribute
                 context:nil].size;
    }
    else{
        theSize=[str
                 sizeWithFont:theFont
                 constrainedToSize:rect
                 lineBreakMode:NSLineBreakByCharWrapping];
    }
    return theSize;
}
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}

- (BOOL)haveNetWork{
    AFNetworkReachabilityManager *reachabilityManager = [AFNetworkReachabilityManager sharedManager];
    BOOL haveNotwork = NO;
    switch (reachabilityManager.networkReachabilityStatus) {
        case AFNetworkReachabilityStatusUnknown:
        case AFNetworkReachabilityStatusNotReachable:
            break;
        case AFNetworkReachabilityStatusReachableViaWWAN:
        case AFNetworkReachabilityStatusReachableViaWiFi:
            haveNotwork = YES;
            break;
    }
    return haveNotwork;
}

+ (BOOL)notificationSettingsIsOpen{
    UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
    if (UIUserNotificationTypeNone == setting.types) {
        return NO;
    }else{
        return YES;
    }
}
+ (BOOL)checkMobil:(NSString *)mobil{
    if ([mobil length]==11) {
        return YES;
    }else{
        return NO;
    }
}
@end
